package com.leesche.zcsp.node.app.v1.controller.login;

import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.utils.web.validation.Regex;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.controller.template.LoginTemplate;
import com.leesche.zcsp.node.web.vo.Account;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.util.Map;

/**
 * @author Jerry Chan
 * @version V1.0
 * @Title: node
 * @Package com.leesche.zcsp.node.app.version1.controller.login
 * @Description:( APP登录控制器 )
 * @date 2016年11月11日 16:36
 */
@Controller
@RequestMapping(value = "/app/v1/login")
public class  AppLoginController extends LoginTemplate {

    private static final Logger logger = Logger
            .getLogger(AppLoginController.class);

    /**
     * @param username 用户名
     * @param password 登录密码
     * @Title: login(String username,String password)
     * @Description: TODO( APP->用户登录 )
     * @author Jerry Chan
     * @date 2016/11/26 18:27
     * @return 登录结果
     */
    @ResponseBody
    @RequestMapping(value = "signin", method = {RequestMethod.POST})
    public Map<String, Object> login(final @RequestParam String username, final @RequestParam String password) {

        /*if (!Regex.isUsername(username)) {
            return WebResult.failure("用户名格式错误").toMap();
        }*/

        /*if (!Regex.isPassword(password)) {
            return WebResult.failure("密码格式错误").toMap();
        }*/

        if (username == null || "".equals(username)) {
            return WebResult.failure("用户名不能为空").toMap();
        }

        if (password == null || "".equals(password)) {
            return WebResult.failure("密码不能为空").toMap();
        }


        try {
            //预留图片验证码校验
            Account accout = new Account();
            accout.setUsername(username);
            accout.setPassword(password);
            return super.userLogin(accout, Constants.MODULE_TYPE_APP);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }

    }

}
